﻿using System.Threading.Tasks;

namespace Dapper.Sharding.Maui
{
    internal class HashSharding<T> : ISharding<T> where T : class
    {
        public HashSharding(ITable<T>[] tableList) : base(tableList)
        {

        }

        public override ITable<T> GetTableById(object id)
        {
            return TableList[ShardingUtils.Mod(id, TableList.Length)];
        }

		public override Task<ITable<T>> GetTableByIdAsync(object id)
		{
            return Task.FromResult(GetTableById(id));
		}

		public override ITable<T> GetTableByModel(T model)
        {
            return TableList[ShardingUtils.Mod(model, SqlField.PrimaryKey, SqlField.PrimaryKeyType, TableList.Length)];
        }

		public override Task<ITable<T>> GetTableByModelAsync(T model)
		{
            return Task.FromResult(GetTableByModel(model));
		}
	}
}
